package com.commerceE.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.commerceE.dao.AddressDao;
import com.commerceE.dao.OrderDao;
import com.commerceE.dao.OrderItemDao;
import com.commerceE.domain.Order;
import com.commerceE.domain.OrderItem;
import com.commerceE.domain.Product_instance;
import com.commerceE.domain.User;
import com.commerceE.service.AddressService;
import com.commerceE.service.OrderService;
import com.commerceE.service.ProductInstanceService;
import com.commerceE.util.Constant;

@Transactional(readOnly = true, rollbackFor = Exception.class)
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private OrderItemDao orderItemDao;
	
	@Autowired
	private AddressService addressService;
	
	@Autowired
	private ProductInstanceService productInstanceService;
	
	public Order getOrderById(Long orderId){
		Order order = orderDao.get(orderId);
		List<OrderItem> orderItemList = orderItemDao.getOrderItemLisByOrderId(orderId);
		order.setOrderItemList(orderItemList);
		return order;
	}
	
	public void updateOrder(Order order){
		orderDao.update(order);
	}
	
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public Order insertOrder(User currentUser, Long addressId){
		Order order = new Order();
		order.setAddressId(addressId);
		order.setOrderprice(currentUser.getTotalPrice());
		order.setPaymentstatus(Constant.PAYMENT_STATUS_UNPAID);
		order.setUserid(currentUser.getUserid());
		Date currentTime = new Date();
		order.setCreatedtime(currentTime);
		order.setLastupdatedtime(currentTime);
		order.setVersion(0);
		
		orderDao.insert(order);
		String orderNo = generateOrderNO(order.getOrderid());
		order.setOrderNo(orderNo);
		orderDao.update(order);
		
		for(Product_instance productInstance : currentUser.getProductInstanceListOfSession()){
			OrderItem orderItem = new OrderItem();
			orderItem.setOrderid(order.getOrderid());
			orderItem.setItermprice(productInstance.getProduct().getDiscountPrice().toString());
			orderItem.setQuantity(productInstance.getQuantity());
			orderItem.setDeliverystatus(Constant.DELIVERY_STATUS_UNDELIVERED);
			orderItem.setCreatedtime(currentTime);
			orderItem.setLastupdatedtime(currentTime);
			orderItem.setProductInstanceId(productInstance.getProductInstanceId());
			orderItem.setVersion(0);
			orderItemDao.insert(orderItem);
			order.getOrderItemList().add(orderItem);
		}
		return order;
	}
	
	public List<Order> getOrderListByUserId(Long userId){
		 List<Order> orderList = orderDao.getOrderListByUserId(userId);
		 for(Order order: orderList){
			 order.setAddress(addressService.getAddressId(order.getAddressId()));
			 List<OrderItem> orderItemList = orderItemDao.getOrderItemLisByOrderId(order.getOrderid());
			 for(OrderItem orderItem:orderItemList){
				 orderItem.setProductInstance(productInstanceService.getProduct_instance(orderItem.getProductInstanceId()));
			 }
			 order.setOrderItemList(orderItemList);
		 }
		 return orderList;
	}
	
	
	private String generateOrderNO(Long orderId){
		int length = 5;
		String defaultStr = "BN";
		StringBuffer maxLongStrBuffer = new StringBuffer("1");
		for(int i=0;i<length;i++){
			maxLongStrBuffer = maxLongStrBuffer.append("0");
		}
		String returnStr = ""+orderId;
		if(orderId>=new Long(maxLongStrBuffer.toString())){
			return defaultStr+orderId+"";
		}else{
			
			int len = returnStr.length();
			for(int i=0;i<length-len;i++){
				returnStr = "0"+returnStr;
			}
		}
		return defaultStr+returnStr;
	}

	public OrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public OrderItemDao getOrderItemDao() {
		return orderItemDao;
	}

	public void setOrderItemDao(OrderItemDao orderItemDao) {
		this.orderItemDao = orderItemDao;
	}
	
	
}
